using System;
using UnityEngine;
using QFramework;

namespace XCZProject
{
	public partial class SimplrAxe : ViewController
	{
		void Start()
		{
			// Code Here
		}

		private float mCurrentSeconds = 0;

		private void Update()
		{
			mCurrentSeconds += Time.deltaTime;

			if (mCurrentSeconds>=1.0f)
			{
				Axe.Instantiate().Show()
					.Position(this.Position())
					.Parent(this)
					.Self(self =>
					{
						var rigidbody2D = self.GetComponent<Rigidbody2D>();

						var randomX = RandomUtility.Choose(-10, -5,-3,3, 5, 10);
						var randomY = RandomUtility.Choose(5, 10);
						rigidbody2D.velocity = new Vector2(randomX, randomY);
						self.OnTriggerEnter2DEvent(collider =>
						{
							var hurtBox = collider.GetComponent<HurtBox>();
							if (hurtBox)
							{
								if (hurtBox.Owner.CompareTag("Enemy"))
								{
									//hurtBox.Owner.GetComponent<IEnemy>().Hurt(2);
									DamageSystem.CalculateDamage(2,hurtBox.Owner.GetComponent<IEnemy>());
								}
							}
						}).UnRegisterWhenGameObjectDestroyed(gameObject);

						ActionKit.OnUpdate.Register(() =>
						{
							if (Player.Default&&Player.Default.Position().y-self.Position().y>15)
							{
								self.DestroyGameObjGracefully();
							}
						}).UnRegisterWhenGameObjectDestroyed(self);
					});
				
				
				mCurrentSeconds = 0;
			}
		}
	}
}
